﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class BC_MDP_ChiTietDAO : ConnectionDAO
    {
        public static bool LapCTBC(long mBC_MDP, long mP)
        {
            OleDbConnection cN = null;
            bool temp = true;

            try
            {
                cN = Cnect();

                string cmString = @"INSERT INTO CHITIET_BC_MDP(MaBC_MDP,MaPhong) VALUES (@mBC_MDP,@mP)";

                OleDbCommand cM = new OleDbCommand(cmString, cN);

                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@mBC_MDP", OleDbType.Integer);
                thamSo.Value = mBC_MDP;
                cM.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@mP", OleDbType.Integer);
                thamSo.Value = mP;
                cM.Parameters.Add(thamSo);


                cM.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                temp = false;
            }
            finally
            {
                if (cN != null && cN.State == System.Data.ConnectionState.Open)
                    cN.Close();
            }

            return temp;
        }

        public static bool Update(long mBC_MDP, long mP, long sN, double tL)
        {
            bool temp = true;
            OleDbConnection cN = null;

            try
            {
                cN = Cnect();

                string cmString = @"UPDATE CHITIET_BC_MDP
                                    SET SoNgayThue=@sN,TyLe=@tL
                                    WHERE MaBC_MDP=@mBC_MDP AND MaPhong=@mP";

                OleDbCommand cM = new OleDbCommand(cmString, cN);

                OleDbParameter thamSo;


                thamSo = new OleDbParameter("@sN", OleDbType.Integer);
                thamSo.Value = sN;
                cM.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@tL", OleDbType.Double);
                thamSo.Value = tL;
                cM.Parameters.Add(thamSo);


                thamSo = new OleDbParameter("@mBC_MDP", OleDbType.Integer);
                thamSo.Value = mBC_MDP;
                cM.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@mP", OleDbType.Integer);
                thamSo.Value = mP;
                cM.Parameters.Add(thamSo);

                cM.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                temp = false;
            }
            finally
            {
                if (cN != null && cN.State == System.Data.ConnectionState.Open)
                    cN.Close();
            }

            return temp;
        }

        public static List<BC_MDP_ChiTietDTO> LayDSTheoMBC(long mBC_MDP)
        {
            OleDbConnection cN = null;
            List<BC_MDP_ChiTietDTO> LCTBC = new List<BC_MDP_ChiTietDTO>();

            try
            {
                cN = Cnect();

                string cmString = @"SELECT CT.MaCT_BC_MDP,CT.MaBC_MDP,CT.MaPhong,CT.SoNgayThue,CT.TyLe,P.TenPhong
                                    FROM CHITIET_BC_MDP CT,PHONG P
                                    WHERE CT.MaBC_MDP=@mBC_MDP AND CT.MaPhong=P.MaPhong
                                    ORDER BY CT.MaPhong asc";


                OleDbCommand cM = new OleDbCommand(cmString, cN);

                OleDbParameter thamSo = new OleDbParameter("@mBC_MDP", OleDbType.Integer);
                thamSo.Value = mBC_MDP;

                cM.Parameters.Add(thamSo);

                OleDbDataReader rDer = cM.ExecuteReader();

                while (rDer.Read())
                {
                    BC_MDP_ChiTietDTO BC = new BC_MDP_ChiTietDTO();

                    if (!rDer.IsDBNull(0))
                        BC.MaCT_BC_MDP = rDer.GetInt32(0);
                    if (!rDer.IsDBNull(1))
                        BC.MaBC_MDP.MaBC_MDP = rDer.GetInt32(1);
                    if (!rDer.IsDBNull(2))
                        BC.MaPhong.MaPhong = rDer.GetInt32(2);
                    if (!rDer.IsDBNull(3))
                        BC.SoNgayThue = rDer.GetInt32(3);
                    if (!rDer.IsDBNull(4))
                        BC.TyLe = rDer.GetDouble(4);
                    if (!rDer.IsDBNull(5))
                        BC.MaPhong.TenPhong = rDer.GetString(5);

                    LCTBC.Add(BC);
                }
            }
            catch (Exception ex)
            {
                LCTBC = new List<BC_MDP_ChiTietDTO>();
            }
            finally
            {
                if (cN != null && cN.State == System.Data.ConnectionState.Open)
                    cN.Close();
            }

            return LCTBC;
        }
    }
}
